#include "SrAudioPlay.h"

#include <QAudioFormat>
#include <QAudioOutput>
#include <mutex>
#include <QThread>

class CXAudioPlay : public SrAudioPlay
{
public:
    QAudioOutput *_output = NULL;
    QIODevice *_io = NULL;
    std::mutex _mux;


public:
    void Close()
    {
        _mux.lock();
        if (_io)
        {
            _io->close();
            _io = NULL;
        }
        if (_output)
        {
            _output->stop();
            delete _output;
            _output = 0;
        }
        _mux.unlock();
    }

    bool Open()
    {
        Close();
        QAudioFormat fmt;
        fmt.setSampleRate(_sampleRate);
        fmt.setSampleSize(_sampleSize);
        fmt.setChannelCount(_channels);
        fmt.setCodec("audio/pcm");
        fmt.setByteOrder(QAudioFormat::LittleEndian);
        fmt.setSampleType(QAudioFormat::UnSignedInt);
        _mux.lock();
        _output = new QAudioOutput(fmt);
        _io = _output->start(); // 开始播放
        _mux.unlock();
        if (!_io) false;
        return true;
    }

    // 播放音频
    bool Write(const unsigned char *data, int datasize)
    {
        if (!data || datasize <= 0) return false;
        _mux.lock();
        if (!_output || !_io)
        {
            _mux.unlock();
            return false;
        }
        int size = _io->write((char*)data, datasize);
        _mux.unlock();
        if (datasize != size)
            return false;
        return true;
    }

    int GetFree()
    {
        _mux.lock();
        if (!_output)
        {
            _mux.unlock();
            return 0;
        }
        int free = _output->bytesFree();
        _mux.unlock();
        return free;
    }

    long long GetNoPlayMs()
    {
        _mux.lock();

        if (!_output)
        {
            _mux.unlock();
            return 0;
        }


        // 一秒音频字节大小
        double secSize = _sampleRate * (_sampleSize / 8) * _channels;


        long long pts = 0;
        // 还未播放的字节数
        double size = _output->bufferSize() - _output->bytesFree();

        if (size <= 0)
        {
            pts = 0;
        }
        else
        {
            pts = (size / secSize) * 1000;
        }

        _mux.unlock();

        return pts;
    }
};



SrAudioPlay* SrAudioPlay::Get()
{
    static CXAudioPlay play;
    return &play;
}

SrAudioPlay::SrAudioPlay()
{
}


SrAudioPlay::~SrAudioPlay()
{
}

